﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.Data;

namespace Pub
{
    public static class CExcel
    {


        public static bool ToExcel(string saveFilePath, DataSet ds)
        {
            return ToExcel(null, saveFilePath, ds);
        }

        public static bool ToExcel(string openFilePath, string saveFilePath, DataSet ds)
        {
            CDebug.EnterMethod("导出Excel");
            try
            {
                ApplicationClass xlsApp = new ApplicationClass();
                if (xlsApp == null)
                {
                    CDebug.Send("机器可能尚未安装Excel,请先安装Excel软件");
                }

                //打开现有的Excel文件 

                Workbook workbook;
                if (openFilePath != null && openFilePath != string.Empty)
                    workbook = xlsApp.Workbooks.Open(openFilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                else
                {
                    xlsApp.Workbooks.Add(true);
                    workbook = xlsApp.ActiveWorkbook;
                }
                Worksheet worksheet = (Worksheet)workbook.Worksheets[1];


                if (!CDB.dsIsNull(ds))
                {
                    //填充表头
                    for (int c = 0; c < ds.Tables[0].Columns.Count; c++)
                    {
                        worksheet.Cells[1, c + 1] = ds.Tables[0].Columns[c].ToString();
                    }

                    //填充数据
                    for (int r = 0; r < ds.Tables[0].Rows.Count; r++)
                    {
                        for (int c = 0; c < ds.Tables[0].Columns.Count; c++)
                        {
                            if (ds.Tables[0].Rows[r][c].ToString() == "")
                                continue;

                            worksheet.Cells[r + 2, c + 1] = ds.Tables[0].Rows[r][c].ToString();
                        }
                    }
                }

                //另存为excel
                workbook.Saved = true;
                workbook.SaveCopyAs(saveFilePath);

                workbook.Close(true, Type.Missing, Type.Missing);
                workbook = null;
                xlsApp.Quit();
                xlsApp = null;

                return true;
            }
            catch (Exception ex)
            {
                CDebug.Send("错误：" + ex.Message);
                CDebug.Send("导出Excel", ex);
                return false;
            }
            finally
            {

                Process[] wordProcess = GetWordProcess();

                foreach (Process pro in wordProcess) //查找没有界面的Word进程
                {
                    IntPtr ip = pro.MainWindowHandle;

                    string str = pro.MainWindowTitle;
                    if (string.IsNullOrEmpty(str))
                    {
                        pro.Kill();
                    }
                }
                CDebug.ExitMethod("导出Excel");
            }

        }

        /// <summary>
        /// 获取所有的WORD进程信息
        /// </summary>
        /// <returns></returns>
        public static Process[] GetWordProcess()
        {
            Process[] wordProcess = Process.GetProcessesByName("Excel");
            if (wordProcess.Length == 0)
                wordProcess = Process.GetProcessesByName("EXCEL");
            if (wordProcess.Length == 0)
                wordProcess = Process.GetProcessesByName("EXCEL.EXE");
            if (wordProcess.Length == 0)
                wordProcess = Process.GetProcessesByName("excel");
            if (wordProcess.Length == 0)
                wordProcess = Process.GetProcessesByName("WINEXCEL");
            if (wordProcess.Length == 0)
                wordProcess = Process.GetProcessesByName("WINEXCEL.EXE");
            if (wordProcess.Length == 0)
                wordProcess = Process.GetProcessesByName("winexcel.exe");

            return wordProcess;
        }
    }
}
